package com.wing.purchase.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.purchase.model.entity.PurchaseType;
import com.wing.purchase.model.request.PurchaseTypeForm;
import com.wing.purchase.service.PurchaseTypeService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-采购类型表管理", tags = "#/purchase/purchaseType")
@RestController("purchaseTypeController")
@RequestMapping("purchase/admin/purchaseType")
@RequiredArgsConstructor
public class PurchaseTypeController {

    private final PurchaseTypeService purchaseTypeService;


    @ApiOperation(value = "查询所有采购类型表", notes = "搜索")
    @GetMapping("/list")
    public PageResult<PurchaseType> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "sort_number") String sort,
            @RequestParam(defaultValue = "asc") String order){
        Page<PurchaseType> pg = new Page<>(page, limit);
        QueryWrapper<PurchaseType> queryWrapper = new QueryWrapper<>();
        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            if (StringUtils.equals("asc", order)) {
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(sort));
            } else {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(sort));
            }
        }
        IPage<PurchaseType> list = purchaseTypeService.getBaseMapper().selectPage(pg, queryWrapper);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "添加采购类型表", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(PurchaseTypeForm.Add.class) PurchaseTypeForm purchaseTypeForm) {
        //purchaseTypeForm.setCreatorName(JwtUtils.getLoginUsername());
        if (purchaseTypeService.add(purchaseTypeForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }




    @ApiOperation(value = "修改采购类型表", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(PurchaseTypeForm.Update.class) PurchaseTypeForm purchaseTypeForm) {
        PurchaseType purchaseType = purchaseTypeService.getById(purchaseTypeForm.getId());
        if(purchaseType == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(PurchaseType.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(purchaseTypeForm, purchaseType, copyOptions);
        if (purchaseTypeService.updateById(purchaseType)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



    @ApiOperation(value = "删除采购类型表", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "purchaseTypeId", value = "采购类型表id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long purchaseTypeId) {
        if (purchaseTypeService.removeById(purchaseTypeId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
}
